package com.fjd.mapper;

import com.fjd.entity.Discount;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface DiscountMapper {

    @Select("select * from discount where merchandise_id = #{merchandiseId} and discount_status = '进行中' ")
    Discount getByMerchandiseIdAndProcessing(String merchandiseId);

    @Select("select * from discount")
    @Results({
            @Result(property = "startTime", column = "start_time", javaType = LocalDateTime.class, jdbcType = JdbcType.TIMESTAMP),
            @Result(property = "endTime", column = "end_time", javaType = LocalDateTime.class, jdbcType = JdbcType.TIMESTAMP),
            @Result(property = "merchandiseId", column = "merchandise_id", javaType = String.class, jdbcType = JdbcType.VARCHAR)
    })
    List<Discount> list();

    @Insert("insert into discount (id,rate,start_time,end_time,merchandise_id,discount_status) VALUES (#{id},#{rate},#{startTime},#{endTime},#{merchandiseId},#{discountStatus})")
    void add(Discount discount);

    @Update("update discount set rate = #{rate}, start_time = #{startTime}, end_time = #{endTime}, discount_status = #{discountStatus} where id = #{id} and merchandise_id = #{merchandiseId}")
    void update(Discount discount);
}
